#!/bin/bash
###########################################################
# Input the Name of Files Generated During Simulation
CASEDAT="	irun.log
			waves.shm
			cov_work
		"
###########################################################
# Input the Dir of SimEnv Files & RTL
export DUT_ROOT="/home/eeeknow/work/ahb2apb/rtl"
export DV_ROOT="/home/eeeknow/work/ahb2apb/dv"

TB="-f $DV_ROOT/etc/tb.f"
DUT="-f $DUT_ROOT/rtl.f"
CaseDir=SDV_ROOT/tc

###########################################################
# Compile & Run options
IRUN_OPT="	+sv
			-uvm
			-access +rw
		"
COV_OPT="	-coverage all
			-covfile $DV_ROOT/../sim/covfile.ccf
			-covdut Cmsdk_ahb_to _apb
			-covoverwrite
		"
###########################################################
# others
ResDir=result
Slog_file=sim.log
Sreport_file=sim.rpt
LOGFILE=irun.log

###########################################################

###########################################################
## sim_one
###########################################################

****** Result for Image/Page 1 ******
FAILED=0
sim_one(){
	TestCaseName=$1
	
	echo "" > $slog_file
	echo "################################################" | tee -a $Slog_file
	echo -n "`date +%F` " | tee -a $Slog_file
	echo "`date +%T`"| tee -a $Slog_file
	echo "BEGIN SIMULATION TESTCASE $TestCaseName ........." | tee -a $Slog_file
	if [ "$SEED_DEF" == "" ]; then
		SEED = `date +%N`
		fi
		
	# Sim prepare
	if [! -d $ResDir/$TestCaseName"_"$SEED ]; then
		mkdir $ResDir/$TestCaseName"_"$SEED
	fi

	for casedatFile in $CASEDAT
	do
		if [ -d $ResDir/$TestCaseName"_"$SEED ; then
			if [ -f $ResDir/$TestCaseName"_"$SEED/$casedatFile ]; then
				rm $ResDir/$TestCaseName"_"$SEED/$casedatFile
			fi
			if [ -d $ResDir/$TestCaseName "_"$SEED/$casedatFile ]; then
				rm -rf $ResDir/$TestCaseName"_"$SEED/$casedatFile
			fi
		fi
	done
	
	# Sim run
	SIM_OPT="$IRUN_OPT -svseed $SEED"
	if [ $COV_DEF ]; then
		SIM_OPT = "$SIM_OPT $COV_OPT"
	fi
	
	echo "SIMULATION SEED :: $SEED" | tee -a $Slog_file | tee -a $Sreport_file
	
	irun $SIM_OPT $DUT $TB +UVM_TESTNAME=$TestCaseName
	# Sim run done
	
cat $LOGFILE >> $slog_file

	pass_string=`sed -n '/^UVM_INFO.*ahb2apb_base_test.sv.*Simulation PASSED!/p' $LOGFILE`

	echo -n "$TestCaseName SIMULATION RESULT:" | tee -a $Slog_file
	if [ "spass_string" != "" ]; then
		echo "PASSED" | tee -a $Slog_file
		FAILED=0
	else
		echo "FAILED" | tee -a $Slog_file
		FAILED=1
	fi
	
	for casedatFile in $CASEDAT
	do
		if [ -d $ResDir/$TestCaseName""$SEED ]; then
			cp $casedatFile $ResDir/$TestCaseName"_"$SEED/. -rf
		fi
	done
	
	echo -n "`date +%F` " | tee -a $Slog_file
	echo "`date +%T`" | tee -a $Slog_file
	echo "END SIMULATION TESTCASE $TestCaseName" | tee -a $Slog_file
	echo "################################################"
	
	cp $Slog_file $ResDir/$TestCaseName"_"$SEED/.
	
	return $FAILED
}
# end of sim_one

#################################################################
## sim_all
#################################################################

err_cnt=0
sim_cnt=0

Sim_All () {

RepeatCount=$1
if [ $RepeatCount -eq 0 ]; then # Do below operations only when
								#it is the 1st round of
								#the whole repeating simulation.
	err_cnt=0
	sim_cnt=0
	
	if [ -d INCA_ libs ]; then
		rm rf INCA_libs
	fi
	
	if [ -d $ResDir ]; then
		rm $ResDir/* rf			#clear the last-time results
	else
		mkdir $ResDir
	fi
	
	if [ -f $Sreport_file ]; then
		echo -n ''> $Sreport_file
	fi
fi

echo "The Testcaes STESTCASE will be simulated !"

i=0
for TestcaseName in $TESTCASE
do
	echo "Simulation $TestcaseName"
	if sim_one $TestcaseName; then
	sim_cnt= expr $sim_cnt + 1
	echo "$TestcaseName PASSED" >»$Sreport_file
	else
		err_cnt=`expr $err_cnt + 1`
		sim_cnt=` expr $sim_cnt +1`
		echo "$TestcaseName FAILED" >>$Sreport_file
	fi
	i= `expr $i + 1`
done

echo "" | tee -a $Sreport_file
echo "(per round): SUMMARY" | tee -a $Sreport_file
echo -n " (per round): ALL SIMULATION $sim_cnt TESTCASE, "| tee -a $Sreport_file

if [ "$sim_cnt" -gt "0" -a "$err_cnt" -eq "0" ]; then
	echo "ALL TESTCASE PASSED!"| tee-a $Sreport_file
else
	echo "$err_cnt TESTCASE FAILED!"| tee -a $Sreport_file
fi
# End of Sim_All
for i
do
	case $1 in
	-a)
		Allsim=1;
		shift;;
	
	-rpt)
		Repeatsim=1; shift;
		if [ $1 != "" ]; then
			RepeatTimes=$1
			shift
		else
			echo "Command Error: No Repeat Times Following the <-rpt> Parameter"
			echo -e $Usage
			exit
		fi
		;;
	-cov)
		COC_DEF=1;shift;;
		
	-seed)
		SEED_DEF=1;shift;
		if [ $1 != "" ]; then
			SEED=$1
			shift
		fi
		;;
	*)
		TESTCASEIN="$TESTCASEIN $1";shift;;
	esac
done

if [ $Allsim ]; then
	ListL1= `ls $CaseDir`
	for FileNameL1 in $ListL1
	do
		testname=${FileNameL1%\.sv} #remove ".sv"
		TESTCASE="$TESTCASE $testname"
	done
elif ["TESTCASEIN" != "" ]; then
	for TestCase in $TESTCASEIN
	do
		TESTCASE="$TESTCASE $TestCase"
	done
fi

i=0
echo "#####Simulate all testcases including :" | tee -a $sreport_file
for TestcaseName in $TESTCASE
do
	echo "###$TestcaseName" | tee -a $Sreport_file
	i= `expr $i + 1`
done

RepeatCount=0
if [ $RepeatSim ]; then
	while [ $RepeatCount -lt $RepeatTimes ]
	do
		Sim_All $RepeatCount
		RepeatCount=`expr $Repeatcount + 1`
		echo "Simulation $RepeatCount times finished !" tee -a $Sreport_file
		echo "#########################################################" | tee -a $Sreport_file
		echo " "| tee -a $Sreport_file
		echo " "| tee -a $Sreport_file
	done
else
	Sim_All $RepeatCount
fi
#########################################
#
# Coverage merging
#
if [ $cov_DEF ]; then
	if [ -d $ResDir/cov_rpt ]; then
		rm $ResDir/cov_rpt/* -rf
	else
		mkdir $ResDir/cov_rpt
	fi

	cd $ResDir
	echo "merge */cov_work/scope/test-output cov_rpt/all" > cov_merge.cf
		iccr cov_merge.cf
		cd -
fi

cp $Sreport_file $ResDir/.

exit